import {
  ValidatorFn,
  AbstractControl,
  ValidationErrors,
} from '@angular/forms';

export class CwsValidators {
  static email (control: AbstractControl): ValidationErrors|null {
    const value = control.value;
    if (isEmptyInputValue(value)) {
      return null;  // don't validate empty values to allow optional controls
    }
    const validEmailReg = /^\w+(\.([\-\w]*)\w+)*@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-])*(\.[A-Za-z]{2,})+$/;
    const valid = validEmailReg.test(value);
    return valid ? null : {invalidEmail: {value}};
  }
}
function isEmptyInputValue(value: any): boolean {
  // we don't check for string here so it also works with arrays
  return value == null || value.length === 0;
}

///**
// * Validator that requires controls to have a value less than a number.
// */
//static max(max: number): ValidatorFn {
//  return (control: AbstractControl): ValidationErrors | null => {
//    if (isEmptyInputValue(control.value)) {
//      return null;  // don't validate empty values to allow optional controls
//    }
//    const value = parseFloat(control.value);
//    return isNaN(value) || value > max ? {'max': {'max': max, 'actual': control.value}} : null;
//  };
//}
//
///**
// * Validator that requires controls to have a non-empty value.
// */
//static required(control: AbstractControl): ValidationErrors|null {
//  return isEmptyInputValue(control.value) ? {'required': true} : null;
//}
//static email (str: string):ValidatorFn {
//  return email (control: AbstractControl): ValidationErrors | null => {
//    const value = control.value;
//    console.log('email', value);
//    if (isEmptyInputValue(value)) {
//      return null;  // don't validate empty values to allow optional controls
//    }
//    const validEmailReg = /^\w+(\.([\-\w]*)\w+)*@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-])*(\.[A-Za-z]{2,})+$/;
//    const valid = validEmailReg.test(value);
//    return valid ? null : {invalidEmail: {value}};
//  }
//}
